/**
 * 
 */
package commons.base.http.client;

import java.io.IOException;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.concurrent.FutureCallback;

/**
 * @author yuan<cihang.yuan@happyelements.com>
 * @since 1.7
 */
public class VersionTest {

	public static void main(String[] args) throws Exception{
		p2();
		
//		get1();
	}
	
	public static void p2(){
		HttpManager.newAsyncGet("http://www.baidu.com").execute(new FutureCallback<Content>() {
			@Override
			public void completed(Content result) {
				System.err.println(result.asString());
			}
			@Override
			public void failed(Exception ex) {
				ex.printStackTrace();
			}
			@Override
			public void cancelled() {
				System.err.println("########");
			}
		});
		System.err.println("=============");
		
//		HttpManager.shutdown();
	}
	
	public static void p1(){
		final Map<String, Integer> counter = new ConcurrentHashMap<String, Integer>();
		final Queue<String> queue = new ConcurrentLinkedQueue<String>();
		
		final CountDownLatch countDownLatch = new CountDownLatch(1000);
		for(int j=10000; j<11000; j++){
			final int localPort = j;
			new Thread(){
				public void run(){
					for(int i=0; i<100; i++){
						try {
							System.err.println(Thread.currentThread() + " : " + i);
							String result = post2("10.130.140.60", localPort);
							String appUrl = parseAppUrl(result);
							queue.offer(appUrl);
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
					countDownLatch.countDown();
				}
			}.start();
		}
//		for(int i=0; i<100000; i++){
//			System.err.println(i);
//			String result = post2("10.130.140.60", 0);
//			String appUrl = parseAppUrl(result);
//			queue.offer(appUrl);
////			if(counter.containsKey(appUrl)){
////				counter.put(appUrl, counter.get(appUrl) + 1);
////			}else{
////				counter.put(appUrl, 1);
////			}
//		}
		
//		countDownLatch.await();
		
		String line = null;
		while ((line = queue.poll()) != null) {
			if (counter.containsKey(line)) {
				counter.put(line, counter.get(line) + 1);
			} else {
				counter.put(line, 1);
			}
		}
		for (String url : counter.keySet()) {
			System.err.println(url + " --> " + counter.get(url));
		}
	}
	
	public static void get1() throws ClientProtocolException, IOException{
		String r = HttpManager.newGet("http://www.baidu.com").execute().asString();
		System.err.println(r);
	}
	
	private static String parseAppUrl(String r){
		String[] ss = r.split(";");
		
		for(String s : ss){
			if(s.startsWith("app_url")){
				return s;
			}
		}
		
		return "";
	}
	
	public static String post2(String localHost, int localPort) throws ClientProtocolException, IOException{
		
		return HttpManager.newPost("http://10.130.130.249:7071/version_checker")
				.setLocalHost(localHost)
				.setLocalPort(localPort)
				.addFormField("platform", "bilibili")
				.addFormField("ver", "1.0")
				.addFormField("build", "446219")
				.addFormField("md5", "1CFAEC6FF9B67CD1ABE92DFCFAD0FE1B")
				.execute().asString();
	}

}
